﻿using HDSmartCommunity.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace HDSmartCommunity.Models
{
    public class ProcessModel
    {
        /// <summary>
        /// 任务脚本ID
        /// </summary>
        public int Id;
        /// <summary>
        /// 任务父ID
        /// </summary>
        public int RId;
        public string Decription;
        public int WaitTime;
        public int TimeOut;
        public string Return;//00|T|0,1|B
        public string Device;
        public string Port;
        public string SendData;
        public string Diffrent;
        public string ConvertType;
        /// <summary>
        /// 此任务时间的持续 T|4000    N次数的持续  N|300   C为子任务
        /// </summary>
        public string Continued;
        /// <summary>
        /// 是否显示
        /// </summary>
        public string Display;
        /// <summary>
        /// 错误后是否继续
        /// </summary>
        public int CanContinue;
        public string UploadData;
        public string OperationId;
        public int RecordSpan;
        public string DisplayDiff;
        public string ExtendData;
        // 新增2021-04-14
        public string Range;
        /// <summary>
        /// 遇到错误继续重试的次数
        /// </summary>
        public int ContinueTime;
        // 跳转到的步数
        public string step;

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    public class Result
    {
        public bool state { get; set; }
        public List<ProcessModel> list { get; set; }
    }

    public enum InfoStatus
    {
        Debug = 0,
        Error = 1,
        Success = 2,
        Info = 3
    }

    public class ReturnData
    {
        public bool IsSuccess;
        public List<ValueData> TestValueData;
    }

    public class ValueData
    {
        public bool IsSuccess;
        public string Id;
        public string Data;
    }

    public class DataModelProcess
    {
        public static List<T> DtToLstModel<T>(DataTable dt) where T : new()
        {
            List<T> lst = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                Type type = typeof(T);
                FieldInfo[] infos = type.GetFields();
                foreach (FieldInfo info in infos)
                {
                    string tName = info.Name;
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (tName.Trim().Equals(dc.ColumnName.Trim()))
                        {
                            if (info.FieldType.IsValueType && (dr[dc.ColumnName] == null || dr[dc.ColumnName].ToString().Equals("")))
                            {
                                info.SetValue(t, 0);
                            }
                            else if (dr[dc.ColumnName].ToString().Equals(""))
                            {
                                info.SetValue(t, "");
                            }
                            else info.SetValue(t, dr[dc.ColumnName]);
                        }
                    }
                }
                lst.Add(t);
            }
            return lst;
        }

        #region 记录脚本异常方法
        public static void ScriptLog(string step, string message)
        {
            Untils.Error(string.Format("{0} 加载脚本出现错误：{1}：{2}", DateTime.Now, step, message));
        }
        #endregion
    }
}
